package com.yjn.bean;

import lombok.Data;
import lombok.ToString;

/**
 * 返回结果实体类
 * @author yujn
 * @date 2020/7/20 10:14
 */
@ToString
@Data
public class Result<T> {

    public final static String OK_CODE = "2000";
    public final static String FAIL_CODE = "5000";
    public final static String OK_MSG = "操作成功";
    public final static String FAIL_MSG = "操作失败";

    private boolean flag;//是否成功
    private String code;//返回码
    private String msg;//返回消息
    private T data;//返回数据

    public Result(boolean flag, String code, String message) {
        this.flag = flag;
        this.code = code;
        this.msg = message;
    }

    public Result(boolean flag, String code, String message, Object data) {
        this.flag = flag;
        this.code = code;
        this.msg = message;
        this.data = (T) data;
    }

    public Result() {

    }

    public static <T> Result<T> success(T data){
        Result result = new Result();
        result.setFlag(Boolean.TRUE);
        result.setCode(OK_CODE);
        result.setMsg(OK_MSG);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success(String msg, T data){
        Result result = new Result();
        result.setFlag(Boolean.TRUE);
        result.setCode(OK_CODE);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> successMsg(String msg){
        Result result = new Result();
        result.setFlag(Boolean.TRUE);
        result.setCode(OK_CODE);
        result.setMsg(msg);
        return result;
    }




    public static <T> Result<T> failure(T data){
        Result result = new Result();
        result.setFlag(Boolean.FALSE);
        result.setCode(FAIL_CODE);
        result.setMsg(FAIL_MSG);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> failureMsg(String msg){
        Result result = new Result();
        result.setFlag(Boolean.FALSE);
        result.setCode(FAIL_CODE);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> failure(String code, String msg){
        Result result = new Result();
        result.setFlag(Boolean.FALSE);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

}
